package com.sf.fy.uav.applet.controller;

import com.sf.fy.uav.common.awt.JwtUtil;
import com.sf.fy.uav.common.awt.RepeatSubmit;
import com.sf.fy.uav.common.request.common.IdRequest;
import com.sf.fy.uav.common.request.user.ReceivingInformationAddRequest;
import com.sf.fy.uav.common.request.user.ReceivingInformationRequest;
import com.sf.fy.uav.common.response.user.ReceivingInformationResponse;
import com.sf.fy.uav.common.response.user.ReceivingInformationRsResponse;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.user.rest.ReceivingInformationRest;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author LTQ
 */
@RestController
@RequestMapping("/sys/receiving-information/")
public class ReceivingInformationController {


    @Autowired
    private ReceivingInformationRest receivingInformationRest;

    @PostMapping("add")
    @Operation(tags = "新增地址", description = "新增地址")
    @RepeatSubmit(interval = 1000, message = "重复提交")
    public Rs<Void> add(@RequestBody ReceivingInformationAddRequest receivingInformationAddRequest) {
        receivingInformationRest.addAddress(receivingInformationAddRequest);
        return Rs.success();
    }

    @PostMapping("edit")
    @Operation(tags = "编辑地址", description = "编辑地址")
    public Rs<Void> edit(@RequestBody @Validated ReceivingInformationRequest receivingInformationRequest) {
        receivingInformationRest.editAddress(receivingInformationRequest);
        return Rs.success();
    }

    @PostMapping("delete")
    @Operation(tags = "删除地址", description = "删除地址")
    public Rs<Void> delete(@RequestBody @Validated IdRequest idRequest) {
        receivingInformationRest.delete(idRequest.getId());
        return Rs.success();
    }

    /**
     * 获取最近n条
     */
    @GetMapping("get-address")
    @Operation(tags = "获取地址，分页", description = "获取地址，分页")
    public Rs<ReceivingInformationRsResponse> getAddress(@RequestParam @NotNull(message = "page不能为空") Integer page, @RequestParam @NotNull(message = "size不能为空") Integer size) {
        String userId = JwtUtil.getUserId();
        ReceivingInformationRsResponse receivingInformationRsResponse = new ReceivingInformationRsResponse();
        List<ReceivingInformationResponse> receivingInformationResponses = receivingInformationRest.getLastTenAddress(userId, 10, page, size);
        receivingInformationRsResponse.setReceivingInformationResponses(receivingInformationResponses);
        Long total = receivingInformationRest.getTotal();
        receivingInformationRsResponse.setTotal(total);
        return Rs.success(receivingInformationRsResponse);
    }

    @GetMapping("get-last-ten")
    @Operation(tags = "获取最后n条地址", description = "获取最后n条地址")
    public Rs<List<ReceivingInformationResponse>> getLastTen(@RequestParam @NotNull(message = "数量不能为空") Integer num) {
        String userId = JwtUtil.getUserId();
        List<ReceivingInformationResponse> receivingInformationResponses = receivingInformationRest.getLastTenAddress(userId, num, null, null);
        return Rs.success(receivingInformationResponses);
    }
}
